import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, DatePicker, Form, Input, InputNumber, message, Modal, Row, Select} from 'antd';
import RoleSelect from "../common/roleSelect";
import BizParams from "../common/BizParams";
import PortSelect from "../common/portSelect";
import Constants from '../../utils/Constants';
import moment from 'moment';

const FormItem = Form.Item;
const R = require("ramda");


const quickBookingDetail = Form.create()(class QuickBookingDetail extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            open: false,
            disabled: false
        }
    }

    handleOpen = (disabled) => {
        this.setState({ open: true, disabled });
    }

    handleCancel = () => {
        this.setState({ open: false });
        this.props.form.resetFields();
    }

    handleConfirm = (status) => {
        this.props.form.validateFields({ force: true }, (errors, values) => {
            if (errors) return;
            if (!values.size20gp && !values.size40gp && !values.size40hq) {
                message.error("请至少填写一种柜子"); return;
            }
            values.id = this.props.quickBooking.dataSource && this.props.quickBooking.dataSource.id ? this.props.quickBooking.dataSource.id : null;
            values.status = status;
            values.loadingTime = values.loadingTime ? values.loadingTime.format(Constants.DATEFORMAT) : undefined;
            if (!R.equals(R.pick(Object.keys(values), this.props.quickBooking.dataSource), values)) {
                this.props.dispatch({
                    type: R.isNil(values.id) ? 'quickBooking/createQuickBooking' : 'quickBooking/updateQuickBooking',
                    payload: {
                        ...values,
                        onSuccess: () => {
                            this.handleCancel();
                            if (this.props.refreshList) this.props.refreshList();
                        }
                    }
                })
            } else this.handleCancel();
        });
    }

    render() {
        let dataSource = this.props.quickBooking.dataSource ? this.props.quickBooking.dataSource : {};
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 14 } };
        let formItemLayout2 = { labelCol: { span: 4 }, wrapperCol: { span: 18 } };
        const { getFieldDecorator } = this.props.form;
        let footer = [
            <Button key="save" size="large" loading={this.props.loading} onClick={this.handleConfirm.bind(this, 1)} style={{ display: this.state.disabled ? 'none' : 'inline-block' }}>保存</Button>,
            <Button key="submit" size="large" loading={this.props.loading} onClick={this.handleConfirm.bind(this, 2)} style={{ display: this.state.disabled ? 'none' : 'inline-block' }}>提交</Button>,
            <Button key="cancel" size="large" loading={this.props.loading} onClick={this.handleCancel.bind(this, true)}>{this.state.disabled ? "关闭" : "取消"}</Button>
        ];

        let supplierList = [];
        let serviceItem = this.props.form.getFieldValue('serviceItems');
        if (serviceItem === '3') supplierList = this.props.supplier.airList ? this.props.supplier.airList : [];
        if (serviceItem === '1' || serviceItem === '2') supplierList = this.props.supplier.supplierList ? this.props.supplier.supplierList : [];
        let berthingCompanyList = this.props.berthingCompany.berthingCompanyList ? this.props.berthingCompany.berthingCompanyList : [];
        return (
            <Modal maskClosable={false} visible={this.state.open} width={1000} title={"订舱详情"} confirmLoading={this.props.loading} onCancel={this.handleCancel.bind(this)} footer={footer}>
                <Form>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="客户名称" hasFeedback>
                                {getFieldDecorator('cstName', {
                                    rules: [{
                                        required: true, message: '请输入客户名称',
                                    }],
                                    initialValue: dataSource.cstName
                                })(
                                    <Input disabled={this.state.disabled} maxLength="200" />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="业务员" hasFeedback>
                                {getFieldDecorator('salesId', {
                                    rules: [{
                                        required: true, message: '请选择业务员',
                                    }],
                                    initialValue: dataSource.salesId ? dataSource.salesId + "" : undefined
                                })(
                                    <RoleSelect role={Constants.ROLE.SALES_MAN} disabled={this.state.disabled} style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="服务类型" hasFeedback>
                                {getFieldDecorator('serviceItems', {
                                    rules: [{
                                        required: true, message: '请选择服务类型',
                                    }],
                                    initialValue: dataSource.serviceItems
                                })(
                                    <BizParams sprCode="bookingType" disabled={this.state.disabled} style={{ width: '100%' }} onChange={() => this.props.form.setFieldsValue({ supplierId: undefined })} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="供应商" hasFeedback>
                                {getFieldDecorator('supplierId', {
                                    rules: [{
                                        required: true, message: '请选择供应商',
                                    }],
                                    initialValue: dataSource.supplierId ? dataSource.supplierId + "" : undefined
                                })(
                                    <Select style={{ width: '100%' }} disabled={this.state.disabled} showSearch placeholder="可通过输入关键字搜索" filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                        {
                                            !R.isNil(supplierList) ? supplierList.map(item => {
                                                return (<Select.Option value={item.id + ""} key={item.id + ""}>{item.supplierCode}</Select.Option>);
                                            }) : ''
                                        }
                                    </Select>
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="船公司" hasFeedback>
                                {getFieldDecorator('berthingCompanyId', {
                                    rules: [{
                                        required: true, message: '请选择船公司',
                                    }],
                                    initialValue: dataSource.berthingCompanyId ? dataSource.berthingCompanyId + "" : undefined
                                })(
                                    <Select style={{ width: '100%' }} disabled={this.state.disabled} showSearch
                                        placeholder="可通过输入关键字搜索"
                                        filterOption={(input, option) => option.props.children.toLowerCase().indexOf(input.toLowerCase()) >= 0}>
                                        {
                                            !R.isNil(berthingCompanyList) ? berthingCompanyList.map(item => {
                                                return (<Select.Option value={item.id + ""}
                                                    key={item.id + ""}>{item.berthingCode}</Select.Option>);
                                            }) : ''
                                        }
                                    </Select>
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="货物名" hasFeedback>
                                {getFieldDecorator('product', {
                                    rules: [{
                                        required: true, message: '请输入货物名',
                                    }],
                                    initialValue: dataSource.product
                                })(
                                    <Input disabled={this.state.disabled} maxLength="200" />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="起运港" hasFeedback>
                                {getFieldDecorator('destination', {
                                    rules: [{
                                        required: true, message: '请选择起运港',
                                    }],
                                    initialValue: dataSource.destination ? dataSource.destination + "" : undefined
                                })(
                                    <PortSelect disabled={this.state.disabled} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="目的港" hasFeedback>
                                {getFieldDecorator('loading', {
                                    rules: [{
                                        required: true, message: '请选择目的港',
                                    }],
                                    initialValue: dataSource.loading ? dataSource.loading + "" : undefined
                                })(
                                    <PortSelect disabled={this.state.disabled} />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="20GP(数量)" hasFeedback>
                                {getFieldDecorator('size20gp', {
                                    initialValue: dataSource.size20gp
                                })(
                                    <InputNumber disabled={this.state.disabled} min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="20GP(重量)" hasFeedback>
                                {getFieldDecorator('weight20gp', {
                                    rules: [{
                                        required: !!this.props.form.getFieldValue("size20gp"), message: '请输入20GP柜子重量',
                                    }],
                                    initialValue: dataSource.weight20gp
                                })(
                                    <InputNumber disabled={this.state.disabled} placeholder="单位：KGS" min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="40GP(数量)" hasFeedback>
                                {getFieldDecorator('size40gp', {
                                    initialValue: dataSource.size40gp
                                })(
                                    <InputNumber disabled={this.state.disabled} min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="40GP(重量)" hasFeedback>
                                {getFieldDecorator('weight40gp', {
                                    rules: [{
                                        required: !!this.props.form.getFieldValue("size40gp"), message: '请输入40GP柜子重量',
                                    }],
                                    initialValue: dataSource.weight40gp
                                })(
                                    <InputNumber disabled={this.state.disabled} placeholder="单位：KGS" min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="40HQ(数量)" hasFeedback>
                                {getFieldDecorator('size40hq', {
                                    initialValue: dataSource.size40hq
                                })(
                                    <InputNumber disabled={this.state.disabled} min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="40HQ(重量)" hasFeedback>
                                {getFieldDecorator('weight40hq', {
                                    rules: [{
                                        required: !!this.props.form.getFieldValue("size40hq"), message: '请输入40HQ柜子重量',
                                    }],
                                    initialValue: dataSource.weight40hq
                                })(
                                    <InputNumber disabled={this.state.disabled} placeholder="单位：KGS" min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="总体积" hasFeedback>
                                {getFieldDecorator('volume', {
                                    rules: [{
                                        required: true, message: '请输入总体积',
                                    }],
                                    initialValue: dataSource.volume
                                })(
                                    <InputNumber disabled={this.state.disabled} placeholder="单位：CBM" min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="总重量" hasFeedback>
                                {getFieldDecorator('weight', {
                                    rules: [{
                                        required: true, message: '请输入总重量',
                                    }],
                                    initialValue: dataSource.weight
                                })(
                                    <InputNumber disabled={this.state.disabled} placeholder="单位：KGS" min={0} maxLength="18" style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="装柜时间" hasFeedback>
                                {getFieldDecorator('loadingTime', {
                                    rules: [{
                                        required: true, message: '请选择装柜时间',
                                    }],
                                    initialValue: dataSource.loadingTime ? moment(dataSource.loadingTime, Constants.DATEFORMAT) : undefined
                                })(
                                    <DatePicker disabled={this.props.disabled} style={{ width: '100%' }} />
                                    )}
                            </FormItem>
                        </Col>
                        <Col span={12}>
                            <FormItem {...formItemLayout} label="备注信息" hasFeedback>
                                {getFieldDecorator('remark', {
                                    initialValue: dataSource.remark
                                })(
                                  // <Input.TextArea disabled={this.state.disabled} maxLength="200" style={{ width: '100%' }}  />
                                    <Input disabled={this.state.disabled} maxLength="2000" />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={24}>
                            <FormItem {...formItemLayout2} label="市场底价" hasFeedback>
                                {getFieldDecorator('floor', {
                                    rules: [{
                                        required: true, message: '请输入市场底价',
                                    }],
                                    initialValue: dataSource.floor
                                })(
                                    <Input.TextArea disabled={this.state.disabled} maxLength="2000" autosize={{ minRows: 3, maxRows: 9 }} />
                                )}
                            </FormItem>
                        </Col>
                    </Row>
                </Form>
            </Modal>
        )
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.quickBooking, quickBooking: state.quickBooking, supplier: state.supplier, berthingCompany: state.berthingCompany
    };
}

export default connect(mapStateToProps)(quickBookingDetail);